﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Ivy.SiteAdmin.BusinessLogic;
using Ivy.SiteAdmin.IBusinessLogic;
using Ivy.SiteAdmin.Entity;

using Ivy.Common.Framework.GeneralUtility;

namespace DasIdeen
{
    public partial class ContentDetails : System.Web.UI.Page
    {
        #region  Variable & constant
        //All constants and local variable should be declared here
        #endregion  Variable & constant

        #region Constructor & Destructor
        //Generally not needed in UI layer
        #endregion Constructor & Destructor

        #region Properties
        //UI layer may have public custom properties. these are very handy for User Controls
        #endregion Properties

        #region Public Methods
        //UI layer may have public custom properties. these are very handy for User Controls
        #endregion Public Methods

        #region Events
        protected void Page_Load(object sender, EventArgs e)
        {
            try //All events in pages or user control should have try{} catch {} block.
            {
                //First check if the current logged in user is having proper role/authorization to access this page or not.
                if (IsUserAutherization())
                {
                    //TODO - Your code to display the initial layout/data etc.
                    InitializeScreen();
                }
                else
                {
                    //TODO - Your code to redirect page to "NOT AUTHORIZED" page or any other page as required by the business logic.
                }
            }
            catch (Exception ex)
            {
                ExceptionUtility.ExceptionHandler(ex, Server.MapPath("~/"));
            }
        }

        #endregion Events

        #region Private Methods

        /// <summary>
        /// This method is to check if the current logged in user is having proper role/authorization to access this page or not.
        /// </summary>
        private bool IsUserAutherization()
        {
            bool _IsUserAutherization = false;
            //TODO - Your code to check authorization goes here.

            //for the time being let us allow the user to access this page.
            //but remove the below line once you have a proper logic.
            _IsUserAutherization = true;

            return _IsUserAutherization;
        }

        /// <summary>
        /// This method is to clear all UI controls which are data binded.
        /// </summary>
        private void ClearScreen()
        {
        }

        /// <summary>
        /// This method is to bind all the UI control like dropdown list or list box etc with the required initial data, 
        /// </summary>
        private void FillLoadLookUpData()
        {
        }

        /// <summary>
        /// This method is to load the UI with initial set of data based of the business logic.
        /// </summary>
        private void InitializeScreen()
        {
            ClearScreen();
            FillLoadLookUpData();

            if (!string.IsNullOrEmpty(Request.QueryString["ContentKey"]))
            {
                int lContentKey = Convert.ToInt32(Request.QueryString["ContentKey"]);
                ContentStatistics lContentStatistics;
                SiteAdminContentManagementBus lSiteAdminContentManagementBus = new SiteAdminContentManagementBus();
                lContentStatistics = lSiteAdminContentManagementBus.GetSelectedContentMetaDataWithContentStatisticsByContentkey(lContentKey);
                this.ContentDetailControl.ActionMode = ScreenActionMode.Display;
                TranslateEntityToUIControls(lContentStatistics);
                //Mrinmoy - Why using wrong variable naming standard???
                string constContentCode = lContentStatistics.ContentCode;
                this.ContentFeedbackControl.LoadAllContentFeedback(constContentCode);
            }
            else
            {
                //Mrinmoy - For new contents.
                if (!string.IsNullOrEmpty(Request.QueryString["ContentTypeCode"]))
                {
                    SiteAdminContentManagementBus lSiteAdminContentManagementBus = new SiteAdminContentManagementBus();
                    this.ContentDetailControl.ContentStatisticsEntity = new ContentStatistics();
                    this.ContentDetailControl.ContentStatisticsEntity.ContentKey = -1;
                    this.ContentDetailControl.ContentStatisticsEntity.ContentTypeCode = Request.QueryString["ContentTypeCode"];
                    this.ContentDetailControl.ContentStatisticsEntity.ContentTypeKey =  lSiteAdminContentManagementBus.GetContentTypeKeyByContentTypeCode(this.ContentDetailControl.ContentStatisticsEntity.ContentTypeCode);
                    //this.ContentDetailControl.ContentStatisticsEntity.ContentName = string.Empty;
                    //this.ContentDetailControl.ContentStatisticsEntity.ContentMetaTagKeyword = string.Empty;
                    //this.ContentDetailControl.ContentStatisticsEntity.ContentMetaTagDescription = string.Empty;
                    //this.ContentDetailControl.ContentStatisticsEntity.ContentHtmlData = string.Empty;
                    
                    this.ContentDetailControl.ContentKey = -1;
                    this.ContentDetailControl.ActionMode = ScreenActionMode.New;
                    TranslateEntityToUIControls(this.ContentDetailControl.ContentStatisticsEntity);
                }
            }
        }
       
        /// <summary>
        /// This method is to copy the entity properties that need to be displayed in the UI controls to the UI control.
        /// Example - [pUser] is an instance of an entity and is filled with specific USER data.
        /// and this user instance is to be displayed is an UI control text boxes.
        /// A sample code will look like the following:
        ///  this.txtFirstName.Text = pUser.UserFirstName; 
        ///  this.txtLastName.Text  = pUser.UserLastName; 
        ///  this.txtLoginId.Text   = pUser.UserLoginID;
        /// </summary>
        private void TranslateEntityToUIControls(ContentStatistics pContentStatistics)
        {
            this.ContentDetailControl.DataBind(pContentStatistics);
        }

        /// <summary>
        /// This method is to copy the user entered data from the UI controls to the entity instance that will be used to sent the entity to any other layer.
        /// Example - [pUser] is an instance of an entity and is to be filled with specific USER data that has been captured in the screen.
        /// A sample code will look like the following:
        ///  pUser.UserFirstName = this.txtFirstName.Text; 
        ///  pUser.UserLastName  = this.txtLastName.Text; 
        ///  pUser.UserLoginID   = this.txtLoginId.Text; 
        /// </summary>
        private void TranslateUIControlsToEntity()
        {
        }
        #endregion Private Methods
    }
}